package com.neusoft.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.neusoft.bean.Power;
import com.neusoft.dao.PowerDao;
import com.neusoft.util.DButil;


public class PowerDaoImpl implements PowerDao{

	@Override
	public List<Power> showPower() {
		List<Power> powerlist = new ArrayList<>();
		DButil db = DButil.getDB();
		Connection conn = db.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("select * from webpower");
			rs = ps.executeQuery();
			while(rs.next()){
				Power power = new Power();
				power.setId(rs.getInt("id"));
				power.setPowername(rs.getString("powername"));
				power.setType(rs.getString("ptype"));
				power.setValue(rs.getString("pvalue"));
				powerlist.add(power);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				rs.close();
				ps.close();
				db.closeConn(conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return powerlist;
	}

	@Override
	public Power findPowerByName(String name) {
		DButil db = DButil.getDB();
		Connection conn = db.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		Power power = new Power();
		try {
			ps = conn.prepareStatement("select * from webpower where powername=?");
			ps.setString(1, name);
			rs = ps.executeQuery();
			if(rs.next()){
				power.setId(rs.getInt("id"));
				power.setPowername(rs.getString("powername"));
				power.setType(rs.getString("ptype"));
				power.setValue(rs.getString("pvalue"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				rs.close();
				ps.close();
				db.closeConn(conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return power;
	}
	
	public Power findPowerById(int id) {
		DButil db = DButil.getDB();
		Connection conn = db.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		Power power = new Power();
		try {
			ps = conn.prepareStatement("select * from webpower where id=?");
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if(rs.next()){
				power.setId(rs.getInt("id"));
				power.setPowername(rs.getString("powername"));
				power.setType(rs.getString("ptype"));
				power.setValue(rs.getString("pvalue"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				rs.close();
				ps.close();
				db.closeConn(conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return power;
	}
	
	
	
}
